﻿using Microsoft.EntityFrameworkCore;
using ProjectCore.Domain.DomainService;
using ProjectCore.Domain.Model.Entity;
using ProjectCore.Domain.Repository.Interfaces;
using ProjectCore.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectCore.Infrastructure.DomainService
{
    public class ProductDomainService : IProductDomainService
    {
        private readonly IBaseRepository<Product> baseRepository;
        private readonly IBaseRepository<SkuCar> skucarbase;
        private readonly IBaseRepository<SkuPhone> skuphonebase;
        private readonly IBaseRepository<Setting> setbase;
        private readonly MyContext myContext;
        public ProductDomainService(IBaseRepository<Product> _baseRepository, MyContext _myContext, IBaseRepository<SkuCar> _skucarbase, IBaseRepository<SkuPhone> _skuphonebase, IBaseRepository<Setting> _setbase)
        {
            baseRepository = _baseRepository;
            myContext = _myContext;
            skucarbase = _skucarbase;
            skuphonebase = _skuphonebase;
            setbase = _setbase;
        }


        //添加
        public async Task<int> AddProduct(Product product)
        {
            try
            {
                return await baseRepository.AddEntityAsync(product);
            }
            catch (Exception) { throw; }
        }


        //反填
        public async Task<Product> GetProduct(int Id)
        {
            try
            {
                return await myContext.Products.FindAsync(Id);
            }
            catch (Exception) { throw; }
        }


        //逻辑删除
        public async Task<int> IsDel(int Id)
        {
            try
            {
                Product product = myContext.Products.Where(x => x.ProID.Equals(Id)).FirstOrDefault();
                product.IsDeleted = true;
                return await myContext.SaveChangesAsync();
            }
            catch (Exception) { throw; }
        }


        //其他设置添加
        public async Task<int> SettingAdd(Setting setting)
        {
            try
            {
                try
                {
                    return await setbase.AddEntityAsync(setting);
                }
                catch (Exception) { throw; }
            }
            catch (Exception)
            {

                throw;
            }

        }


        //其他设置反填
        public async Task<Setting> GetSetting(int Id)
        {
            try
            {
                return await myContext.Settings.FindAsync(Id);
            }
            catch (Exception)
            {

                throw;
            }
            
        }


        //其他设置修改
        public async Task<int> SettingUpt(Setting setting)
        {
            try
            {
                return await setbase.UpdateEntityAsync(setting);
            }
            catch (Exception) { throw; }
        }


        //汽车Sku添加
        public async Task<int> SkuCarAdd(SkuCar car)
        {
            try
            {
                return await skucarbase.AddEntityAsync(car);
            }
            catch (Exception) { throw; }
        }


        //汽车Sku反填
        public async Task<SkuCar> GetCar(int Id)
        {
            try
            {
                return await myContext.SkuCars.FindAsync(Id);
            }
            catch (Exception)
            {

                throw;
            }
        }


        //汽车Sku修改
        public async Task<int> SkuCarUpt(SkuCar car)
        {
            try
            {
                return await skucarbase.UpdateEntityAsync(car);
            }
            catch (Exception) { throw; }
        }


        //手机Sku添加
        public async Task<int> SkuPhoneAdd(SkuPhone phone)
        {
            try
            {
                return await skuphonebase.AddEntityAsync(phone);
            }
            catch (Exception) { throw; }
        }


        //手机Sku反填
        public async Task<SkuPhone> GetPhone(int Id)
        {
            return await myContext.SkuPhones.FindAsync(Id);
        }


        //手机Sku修改
        public async Task<int> SkuPhoneUpt(SkuPhone phone)
        {
            try
            {
                return await skuphonebase.UpdateEntityAsync(phone);
            }
            catch (Exception) { throw; }
        }


        //修改
        public async Task<int> UptProduct(Product product)
        {
            try
            {
                return await baseRepository.UpdateEntityAsync(product);
            }
            catch (Exception) { throw; }
        }

    }
}
